
var util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    tc: 0,

    orgInfo:'',

    page:1,
    limit:10,
    topicType: 8,//默认是 近况（相册）
    albumid:'',//子分类
    postuid:'',
    data:[],
    imgDomain: app.globalData.imgDomain,
    hasMore: true,
    hasLoadding: false,
    noData: false,

    wz_item_img_w_1: 300,
    wz_item_img_w_2: 300,
    wz_item_img_w_3: 300
  },

  onLoad:function(options){

    var that = this;
    var tc = app.get_tc();
    that.setData({
      tc: tc,
      orgname:''
    })
   
    var titleName = tc == 2 ?"WeChat authorized login":tc == 1 ? '微信授權登錄' : '微信授权登录';
    titleName
    //console.log(util.orgname);
    
    wx.setNavigationBarTitle({
      // title: options.title,
      title: titleName,
    });

    //获取小程序信息
    app.GetUserInfo(util.orgid, function(res){
      console.log("小程序信息：", res);
      that.setData({
        orgInfo: res
      })
    });

    //获取颜色配置
    app.getColorSetData(function (color) {
      that.setData({
        colorSet: color
      })
      that.resetNavColor();
    });

  },
  

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.navigateTo({ url: url })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

   onGotUserInfo: function(e) {
    console.log(e);
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
    // if(e.detail.errMsg == 'getUserInfo:fail auth deny'){
    //   console.log("用户拒绝了授权");
      
    // }else{
      
    // }
    //当前登录用户信息
    var that=this
    that.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (!authFail) {
        //更新数据
        that.setData({
          userInfo: res,
          uid: res.uid,
          token: res.token,
          openid: res.wxsmall_openid
        });
      }
      that.navigateBack()
    });
     

  },

  resetNavColor: function () {
    var bg_color = '#d8202d';
    var fg_color = '#ffffff';

    if (this.data.colorSet) {
      var colorJO = this.data.colorSet;
      bg_color = colorJO.titlebg;
      fg_color = colorJO.titlefont;
    }
    wx.setNavigationBarColor({
      frontColor: fg_color,
      backgroundColor: bg_color
    });
    this.setData({
      backgroundColor: bg_color
    })
  },
  navigateBack: function () {
    wx.navigateBack({
      delta: 1
    })
  },

  LoginUserInfo: function (cb, inviteuid = 0) {
    var that = this
    if (!inviteuid || inviteuid == 'undefined') {
      inviteuid = 0;
    }
    //先判断缓存中有无数据
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      that.globalData.userInfo = userInfo;
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用微信登录接口  
      wx.login({
        success: function (e) {
          wx.getUserInfo({
            success: function (res) {
              console.log(222);
              console.log(res);
              var js_code = e.code;
              console.log("js_code:" + js_code);
              //小程序登录地址
              // var loginUrl = util.api_host + '/index.php?app=w3g&mod=Public&act=loginWechatSmall';
              //会新建用户的 登录请求地址
              // var loginUrl = util.api_host + '/index.php?app=w3g&mod=Public&act=loginWechatSmallCon&binduid=' + util.orgid + '&appid=' + util.appid;
              var loginUrl = util.api_host + '/index.php?app=w3g&mod=Public&act=loginWechatSmallCon&binduid=' + util.orgid;
              var params = {
                "code": js_code,
                "encryptedData": res.encryptedData,
                "iv": res.iv,
                "inviteuid": inviteuid
              };
              console.log("请求地址：" + loginUrl, params);
              wx.request({
                url: loginUrl,
                data: params,
                method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                // header: {}, // 设置请求的 header
                success: function (response) {
                  console.log(333);
                  console.log(response);
                  if (response.data.status == 1) {
                    //登录成功，存储信息                    
                    that.data.userInfo = response.data.data;
                    wx.setStorageSync('userInfo', response.data.data);
                    console.log(that.globalData);
                    typeof cb == "function" && cb(that.data.userInfo)

                    // 登录页面
                    if (util.orgid == 13078) {
                      var path = "/pages/xtt-denglu/xtt-denglu";

                      wx.redirectTo({
                        url: path,
                      })
                    }
                  } else {
                    wx.showToast({
                      title: response.data.message,
                      icon: 'error',
                      duration: 2000
                    })
                  }
                },
                fail: function (e) {
                  // fail
                  console.log("wx.request:", e);
                }
              })
            },
            fail: function () {

              // var path = "/pages/authorization/authorization";
              // wx.navigateTo({
              //   url: path,
              // })
              console.log("wx.getUserInfo fail:", e);
              typeof cb == "function" && cb(-1);
            }
          })
        },
        fail: function (e) {
          console.log("wx.login fail:", e);
        }
      });
    }
  },
})